package com.travelshare.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.travelshare.common.core.controller.BaseController;
import com.travelshare.common.core.domain.AjaxResult;
import com.travelshare.common.core.domain.entity.SysUser;
import com.travelshare.common.core.domain.model.RegisterBody;
import com.travelshare.common.utils.SecurityUtils;
import com.travelshare.common.utils.StringUtils;
import com.travelshare.framework.web.service.SysRegisterService;
import com.travelshare.system.service.ISysConfigService;
import com.travelshare.system.service.ISysUserService;

/**
 * 注册验证
 * 
 * @author travelshare
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;
    
    @Autowired
    private ISysUserService userService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
    
    @PostMapping("/appRegister")
    public AjaxResult appRegister(@RequestBody SysUser user)
    {
    	if(user.getUserName().contains("admin") || user.getUserName().contains("test") || user.getUserName().contains("ceshi")) {
    		return error("注册用户'" + user.getUserName() + "'失败，登录账号不合规");
    	}
    	if (!userService.checkUserNameUnique(user))
        {
            return error("注册用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("注册用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("注册用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setCreateBy("手机端注册");
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setRoleIds(new Long[] { 2l });
        return toAjax(userService.insertUser(user));
    }
}
